package com.ujoin.dao.imp;

import java.sql.Timestamp;
import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ujoin.bean.Comment;
import com.ujoin.dao.ICommentDAO;

/**
 * A data access object (DAO) providing persistence and search support for
 * Comment entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.ujoin.bean.Comment
 * @author MyEclipse Persistence Tools
 */

public class CommentDAO extends HibernateDaoSupport implements ICommentDAO {
	private static final Logger log = LoggerFactory.getLogger(CommentDAO.class);
	// property constants
	public static final String ACTIVITY_ID = "activityId";
	public static final String USER_ID = "userId";
	public static final String COMMENT = "comment";
	public static final String VALID = "valid";

	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.ICommentDAO#save(com.ujoin.bean.Comment)
	 */
	@Override
	public void save(Comment transientInstance) {
		log.debug("saving Comment instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.ICommentDAO#delete(com.ujoin.bean.Comment)
	 */
	@Override
	public void delete(Comment persistentInstance) {
		log.debug("deleting Comment instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.ICommentDAO#findById(java.lang.Integer)
	 */
	@Override
	public Comment findById(java.lang.Integer id) {
		log.debug("getting Comment instance with id: " + id);
		try {
			Comment instance = (Comment) getHibernateTemplate().get(
					"com.ujoin.bean.Comment", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.ICommentDAO#findByExample(com.ujoin.bean.Comment)
	 */
	@Override
	public List findByExample(Comment instance) {
		log.debug("finding Comment instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.ICommentDAO#findByProperty(java.lang.String, java.lang.Object)
	 */
	@Override
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Comment instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Comment as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.ICommentDAO#findByActivityId(java.lang.Object)
	 */
	@Override
	public List findByActivityId(Object activityId) {
		return findByProperty(ACTIVITY_ID, activityId);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.ICommentDAO#findByUserId(java.lang.Object)
	 */
	@Override
	public List findByUserId(Object userId) {
		return findByProperty(USER_ID, userId);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.ICommentDAO#findByComment(java.lang.Object)
	 */
	@Override
	public List findByComment(Object comment) {
		return findByProperty(COMMENT, comment);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.ICommentDAO#findByValid(java.lang.Object)
	 */
	@Override
	public List findByValid(Object valid) {
		return findByProperty(VALID, valid);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.ICommentDAO#findAll()
	 */
	@Override
	public List findAll() {
		log.debug("finding all Comment instances");
		try {
			String queryString = "from Comment";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.ICommentDAO#merge(com.ujoin.bean.Comment)
	 */
	@Override
	public Comment merge(Comment detachedInstance) {
		log.debug("merging Comment instance");
		try {
			Comment result = (Comment) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.ICommentDAO#attachDirty(com.ujoin.bean.Comment)
	 */
	@Override
	public void attachDirty(Comment instance) {
		log.debug("attaching dirty Comment instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.ICommentDAO#attachClean(com.ujoin.bean.Comment)
	 */
	@Override
	public void attachClean(Comment instance) {
		log.debug("attaching clean Comment instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static ICommentDAO getFromApplicationContext(ApplicationContext ctx) {
		return (ICommentDAO) ctx.getBean("CommentDAO");
	}
}